var listOfChords = new Array("A","B","C","D","E","F","G");
var listOfMissedMajorChords = new Array(0,0,0,0,0,0,0);
var listOfMissedMinorChords = new Array(0,0,0,0,0,0,0);


//Statistics
var correctAnswers = 0;
var correctInARow = 0;
var totalAnswers = 0;
var recordInARow = 0;
var timesWrongGuess = 0;

var answer;

var majorChords;

//var listOfMajorChords;
//var listOfMinorChords;

$(document).ready(function() {

    $('#button').html('<input type="button" value="New chord" onclick="playRandomChord()" />');
    $('#options').html('<h2>Chord types</h2><input type="button" value="Major chords" onclick="setupMajorChords()" disabled /><input type="button" value="Minor chords" onclick="setupMinorChords()" />')

    majorChords = true;

    updateStatistics();

    //Load songs
    soundManager.url = '';
    soundManager.debugMode = false;
    soundManager.onload = function() {
        aChord = soundManager.createSound({
            id: 'aChord',
            url: 'sound/aChord.mp3'
        });
        bChord = soundManager.createSound({
            id: 'bChord',
            url: 'sound/bChord.mp3'
        });
        cChord = soundManager.createSound({
            id: 'cChord',
            url: 'sound/cChord.mp3'
        });
        dChord = soundManager.createSound({
            id: 'dChord',
            url: 'sound/dChord.mp3'
        });
        eChord = soundManager.createSound({
            id: 'eChord',
            url: 'sound/eChord.mp3'
        });
        fChord = soundManager.createSound({
            id: 'fChord',
            url: 'sound/fChord.mp3'
        });
        gChord = soundManager.createSound({
            id: 'gChord',
            url: 'sound/gChord.mp3'
        });

        aMinor = soundManager.createSound({
            id: 'aMinor',
            url: 'sound/aMinor.mp3'
        });
        bMinor = soundManager.createSound({
            id: 'bMinor',
            url: 'sound/bMinor.mp3'
        });
        cMinor = soundManager.createSound({
            id: 'cMinor',
            url: 'sound/cMinor.mp3'
        });
        dMinor = soundManager.createSound({
            id: 'dMinor',
            url: 'sound/dMinor.mp3'
        });
        eMinor = soundManager.createSound({
            id: 'eMinor',
            url: 'sound/eMinor.mp3'
        });
        fMinor = soundManager.createSound({
            id: 'fMinor',
            url: 'sound/fMinor.mp3'
        });
        gMinor = soundManager.createSound({
            id: 'gMinor',
            url: 'sound/gMinor.mp3'
        });

        listOfMajorChords = new Array(aChord, bChord, cChord, dChord, eChord, fChord, gChord);
        listOfMinorChords = new Array(aMinor, bMinor, cMinor, dMinor, eMinor, fMinor, gMinor);
    }
});


function updateStatistics() {
    $('#stats').html('<h2>Statistics</h2><p>Right guesses: ' + correctAnswers + '/' + totalAnswers +'</p> <p>Correct in a row: ' + correctInARow + '</p>' + '<p>Best in-a-row record: ' + recordInARow + '</p><p>Missed attemps per chord before getting it right:<ul>');
    for(i = 0; i < listOfMissedMajorChords.length; i++) {
        if(majorChords) {
            if(listOfMissedMajorChords[i] != 0) {
                $('#stats').append('<li>'+listOfChords[i] + ': ' + listOfMissedMajorChords[i] +'</li>');
            }
        }
        else {
            if(listOfMissedMinorChords[i] != 0) {
                $('#stats').append('<li>'+listOfChords[i] + 'm: ' + listOfMissedMinorChords[i] +'</li>');
            }
        }
    }
    $('#stats').append('<ul>')
}

function playRandomChord() {
    $('#answer').css('border','0');
    $('#button').html('<input type="button" value="Listen again" onclick="listenAgain()" />');
    $('#input').html('Answer: ');
    $('#answer').html('');
    
    for(i=0; i < listOfChords.length; i++) {
        if(majorChords) {
            $('#input').append('<input type="button" class="answer" value="' + intToAlphabet(i) + '" onclick="answerGuessed(' + i +')" />');
        }
        else {
            $('#input').append('<input type="button" class="answer" value="' + intToAlphabet(i) + 'm" onclick="answerGuessed(' + i +')" />');
        }
        
    }

    var randValue = Math.floor(Math.random() * listOfChords.length);
    answer = randValue;

    if(majorChords) {
        listOfMajorChords[answer].play();
    }
    else {
        listOfMinorChords[answer].play();
    }
    return listOfChords[answer];
}

function answerGuessed(number_string) {
    var number = parseInt(number_string);

    totalAnswers++;

    if(answer == number) {
        $('#answer').html('Correct!');
        $('#answer').css('border','1px solid green');
        $('#input').html('');
        $('#button').html('<input type="button" value="New chord" onclick="playRandomChord()" />');
        correctAnswers++;
        correctInARow++;
        if(recordInARow < correctInARow) {
            recordInARow = correctInARow;
        }

        if(majorChords) {
            listOfMissedMajorChords[answer] += timesWrongGuess;
        }
        else {
            listOfMissedMinorChords[answer] += timesWrongGuess;
        }
        timesWrongGuess = 0;

    }
    else {
        correctInARow=0;
        timesWrongGuess++;

        if(majorChords) {
            listOfMajorChords[number].play();
            $('#answer').html('That the ' + intToAlphabet(number) + '-chord, and it sounds like this. Incorrect answer.');
        }
        else {
            listOfMinorChords[number].play();
            $('#answer').html('That the ' + intToAlphabet(number) + 'm-chord, and it sounds like this. Incorrect answer.');
        }
        $('#answer').css('border','1px solid red');
    }
    
    updateStatistics();

}

function listenAgain() {
    if(majorChords) {
        listOfMajorChords[answer].play();
    }
    else {
        listOfMinorChords[answer].play();
    }
}

function intToAlphabet(number) {
    switch(number) {
        case 0:
            return "A";
        case 1:
            return "B";
        case 2:
            return "C";
        case 3:
            return "D";
        case 4:
            return "E";
        case 5:
            return "F";
        case 6:
            return "G";
            return nil;
    }
}

function getAnswer() {
    return listOfChords[answer];
}

function setupMajorChords() {
    $('#options').html('<h2>Chord types</h2><input type="button" value="Major chords" onclick="setupMajorChords()" disabled /><input type="button" value="Minor chords" onclick="setupMinorChords()" />')
    majorChords = true;
    timesWrongGuess = 0;
    $('#input').html('');
    $('#answer').html('');
    $('#answer').css('border','0');
    $('#button').html('<input type="button" value="New chord" onclick="playRandomChord()" />');
    updateStatistics();
}

function setupMinorChords() {
    $('#options').html('<h2>Chord types</h2><input type="button" value="Major chords" onclick="setupMajorChords()" /><input type="button" value="Minor chords" onclick="setupMinorChords()" disabled />')
    majorChords = false;
    timesWrongGuess = 0;
    $('#input').html('');
    $('#answer').html('');
    $('#answer').css('border','0');
    $('#button').html('<input type="button" value="New chord" onclick="playRandomChord()" />');
    updateStatistics();
}